/**
 * 
 */
package com.arx.character;

import com.arx.common.Common;
import com.arx.common.GlobalConsts;
import com.arx.interactive.Interactive;
import com.arx.script.Script;

/**
 * @author Donald
 *
 */
abstract class PlayerStatsBase extends PlayerCommonVars {
	/**
	 * Add gold to player purse.
	 * @param val the value of the gold
	 */
	public final void ARX_PLAYER_AddGold(final long val) {
		super.getPlayer().setGold(super.getPlayer().getGold() + val);
		// set the flag to display a halo over gold
		bGoldHalo = true;
		// start the halo's timer
		ulGoldHaloTime = 0;
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public final void ARX_PLAYER_AddBag() {
		if (super.getPlayer().getBag() < 3) {
			super.getPlayer().setBag((short) (super.getPlayer().getBag() + 1));
		}
	}
	protected void ARX_PLAYER_ComputePlayerStats() {
		super.getPlayer().setMaxlife(super.getPlayer().getAttribute_Constitution()
				* (super.getPlayer().getLevel() + 2));
		super.getPlayer().setMaxmana(super.getPlayer().getAttribute_Mind() * (super.getPlayer().getLevel() + 1));
		float t = ARX_PLAYER_Get_Skill_Defense(player, (byte) 0);

		float fCalc = t * GlobalConsts.DIV10 - 1;
		fCalc = Common.getInstance().checkUnsignedCharValue(fCalc);
		super.getPlayer().setArmor_class((short) fCalc);

		if (super.getPlayer().getArmor_class() < 1) {
			super.getPlayer().setArmor_class((short) 1);
		}

		super.getPlayer().setDamages(100);
		fCalc = (super.getPlayer().getAttribute_Mind() * 2.f * (1.f + ARX_PLAYER_Get_Skill_Casting(
				player, (byte) 0)
				* GlobalConsts.DIV200));
		fCalc = Common.getInstance().checkUnsignedCharValue(fCalc);
		super.getPlayer().setResist_magic((short) fCalc);

		fCalc = super.getPlayer().getAttribute_Constitution()
				* 2
				+ ARX_PLAYER_Get_Skill_Defense(player, (byte) 1)
				* GlobalConsts.DIV4;
		fCalc = Common.getInstance().checkUnsignedCharValue(fCalc);
		super.getPlayer().setResist_poison((short) fCalc);

		super.getPlayer().setDamages((super.getPlayer().getAttribute_Strength() - 10)
				* GlobalConsts.DIV2);

		if (super.getPlayer().getDamages() < 1.f) {
			super.getPlayer().setDamages(1.f);
		}

		super.getPlayer().setAimTime(1500);
	}
	/**
	 * Manages Player Level Up event.
	 * @param player
	 */
	private void ARX_PLAYER_LEVEL_UP() {
		// ARX_SOUND_PlayInterface(SND_PLAYER_LEVEL_UP);
		super.getPlayer().incrementLevel();
		super.getPlayer().adjustSkill_Redistribute((short) 15);
		super.getPlayer().adjustAttribute_Redistribute((short) 1);
		ARX_PLAYER_ComputePlayerStats();
		super.getPlayer().setLife(super.getPlayer().getMaxlife());
		super.getPlayer().setMana(super.getPlayer().getMaxmana());
		super.getPlayer().setOld_Skill_Stealth(super.getPlayer().getSkill_Stealth());
		super.getPlayer().setOld_Skill_Mecanism(super.getPlayer().getSkill_Mecanism());
		super.getPlayer().setOld_Skill_Intuition(super.getPlayer().getSkill_Intuition());
		super.getPlayer().setOld_Skill_Etheral_Link(super.getPlayer().getSkill_Etheral_Link());
		super.getPlayer().setOld_Skill_Object_Knowledge(super.getPlayer().getSkill_Object_Knowledge());
		super.getPlayer().setOld_Skill_Casting(super.getPlayer().getSkill_Casting());
		super.getPlayer().setOld_Skill_Projectile(super.getPlayer().getSkill_Projectile());
		super.getPlayer().setOld_Skill_Close_Combat(super.getPlayer().getSkill_Close_Combat());
		super.getPlayer().setOld_Skill_Defense(super.getPlayer().getSkill_Defense());

		Script.getInstance().SendIOScriptEvent(
						Interactive.getInstance().getInteractiveObject(0), 
						0, 
						"", 
						"LEVEL_UP"
						);
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public final void ARX_PLAYER_Modify_XP(final long val) {
		super.getPlayer().adjustXp(val);
		for (byte i = 1; i < 11; i++) {
			boolean lvup = false;
			if (i > super.getPlayer().getLevel()) {
				if (super.getPlayer().getXp() >= GetXPforLevel(i)) {
					lvup = true;
				}
				if (lvup) {
					ARX_PLAYER_LEVEL_UP();
				}
			}
		}
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public final long GetXPforLevel(final int level) {
		int val = Common.LONG_MAX;
		switch (level) {
		case 0:
			val = 0;
			break;
		case 1:
			val = 2000;
			break;
		case 2:
			val = 4000;
			break;
		case 3:
			val = 6000;
			break;
		case 4:
			val = 10000;
			break;
		case 5:
			val = 16000;
			break;
		case 6:
			val = 26000;
			break;
		case 7:
			val = 42000;
			break;
		case 8:
			val = 68000;
			break;
		case 9:
			val = 110000;
			break;
		case 10:
			val = 178000;
			break;
		case 11:
			val = 300000;
			break;
		case 12:
			val = 450000;
			break;
		case 13:
			val = 600000;
			break;
		case 14:
			val = 750000;
			break;
		default:
			val = level * 60000;
			break;
		}
		return val;
	}
}
